package tools;
import java.io.*;
import java.net.*;
public class JURL_crawler
{
    public static void crawler(String url_address,String file_path,String encoding){
        try{
            URL url=new URL(url_address);//创建资源类型
            String protocol=url.getProtocol();//获取资源类型
            String host=url.getHost();//获取域名
            int port=url.getPort();//获取端口
            String file=url.getFile();//获取路径
            //System.out.println("url地址的资源类型为："+protocol+"域名为："+host+"端口为:"+port+"路径为:"+file);
            InputStream is=url.openStream();//获取页面信息流
            BufferedReader bfr=new BufferedReader(new InputStreamReader(is,encoding));//封装成字符流
    	    BufferedWriter ww=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file_path),encoding));
            String len;
            while((len=bfr.readLine())!=null)
            {
                //System.out.println(len);
    	    ww.write(len+"\n");
            }
            bfr.close();
            is.close();
    	ww.close();
        }
        catch(MalformedURLException e){
            System.out.println("创建URL对象发生异常");
        }
        catch(IOException e){
            System.out.println("发生IO操作异常");
        }
    }
    public static void main(String args[]){
        JURL_crawler.crawler("http://mip.chinaz.com/Ip/IpSame/?query=120.196.134.137","chinaz.txt","utf-8");
    }
}
